package com.mp3s.dc.model;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import com.mp3s.dc.service.Config;

@Service
public class XMLService {
   private final ResourceLoader resourceLoader;

    public XMLService(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    public String parseXmlFile(String filePath) throws Exception {
        // 加载外部 XML 文件
        Resource resource = resourceLoader.getResource("file:" + filePath);
        InputStream inputStream = resource.getInputStream();

        // 使用 JAXB 解析 XML
        JAXBContext jaxbContext = JAXBContext.newInstance(XML.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        XML cfg = (XML) unmarshaller.unmarshal(inputStream);
        Config.set(cfg.getSharePath(), cfg.getSavePath(),cfg.getLogFile());
        return cfg.toString();
    }
}
